package com.douqu.game.core.entity.battle;


import com.bean.core.buffer.ByteBuffer;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-03-05 18:01
 */
public class SpriteTemp extends ConfigTemp {

    /**
     * index为-1的时候表示忽略此属性，用在玩家身上，因为玩家是不允许卡组里有重复卡的
     */
    public final static int IGNORE_INDEX = -1;
    /**
     * 索引（主要用来处理Monster里的,这样才能一一对应）
     */
    private int index;

    private long hp;

    private long maxHp;

    private long cacheMaxHp;

    private long toEnemyDamage;

    public SpriteTemp() {
    }

    public void writeTo(ByteBuffer buffer){
        buffer.writeInt(index);
        buffer.writeLong(hp);
        buffer.writeLong(maxHp);
        buffer.writeLong(cacheMaxHp);
        buffer.writeLong(toEnemyDamage);
    }

    public void loadFrom(ByteBuffer buffer){
        index = buffer.readInt();
        hp = buffer.readLong();
        maxHp = buffer.readLong();
        cacheMaxHp = buffer.readLong();
        toEnemyDamage = buffer.readLong();
    }

    public SpriteTemp(int configId, int lv, long hp, long maxHp, long cacheMaxHp, int index)
    {
        super(configId, lv);

        this.hp = hp;
        this.maxHp = maxHp;
        this.cacheMaxHp = cacheMaxHp;
        this.index = index;
    }

    public SpriteTemp(SpriteTemp spriteTemp)
    {
        super(spriteTemp.getId(), spriteTemp.getLv());

        this.hp = spriteTemp.getHp();
        this.maxHp = spriteTemp.getMaxHp();
        this.cacheMaxHp = spriteTemp.getCacheMaxHp();
        this.index = spriteTemp.getIndex();
    }

    public int getHPRate()
    {
        if(maxHp == 0)
            return 0;

        return (int) ((float) hp / maxHp * 100);
    }

    public void addHPByRate(double rate)
    {
        hp += (long) (maxHp * rate);
        hp = hp < 0 ? 0 : hp;
        hp = hp > maxHp ? maxHp : hp;
    }

    public void addHP(int value)
    {
        hp += value;
        hp = hp < 0 ? 0 : hp;
        hp = hp > maxHp ? maxHp : hp;
    }

    public void merge(SpriteTemp spriteTemp)
    {
        maxHp += spriteTemp.getMaxHp();
        hp += spriteTemp.getHp();
    }

    public boolean isMax()
    {
        return hp >= maxHp;
    }

    public void full()
    {
        hp = maxHp;
    }


    public long getHp() {
        return hp;
    }

    public void setHp(long hp) {
        this.hp = hp;
    }

    public long getMaxHp() {
        return maxHp;
    }

    public void setMaxHp(long maxHp) {
        this.maxHp = maxHp;
    }

    public long getCacheMaxHp() {
        return cacheMaxHp;
    }

    public void setCacheMaxHp(long cacheMaxHp) {
        this.cacheMaxHp = cacheMaxHp;
    }

    public long getToEnemyDamage() {
        return toEnemyDamage;
    }

    public void setToEnemyDamage(long toEnemyDamage) {
        this.toEnemyDamage = toEnemyDamage;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    @Override
    public String toString() {
        return "{" +
                "index=" + index +
                ", hp=" + hp +
                ", maxHp=" + maxHp +
                ", cacheMaxHp=" + cacheMaxHp +
                ", toEnemyDamage=" + toEnemyDamage +
                "} " + super.toString();
    }
}
